package com.jiyici;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jiyici.controller.DataManager;
import com.jiyici.controller.DataManager.DataListener;
import com.jiyici.fragment.CategoryListFragment;
import com.jiyici.fragment.ItemListFragment;
import com.jiyici.utils.Global;

public class HomeActivity extends SherlockFragmentActivity implements DrawerListener {

    private DrawerLayout mDrawerLayout;

    private BroadcastReceiver mDataChangeBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DataManager.ACTION_DATA_CHANGED.equals(action)) {
                int dataType = intent.getIntExtra(DataManager.EXTRA_KEY_DATA_TYPE, 0);
                if (dataType == DataManager.DATA_CHANGED_CATEGORY_ITEM) {
                    DataListener listener = (DataListener) getSupportFragmentManager().findFragmentById(
                            R.id.fragment_container);
                    listener.onItemCatgoryChanged();
                }
            }
        }

    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            switchDrawer();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /* implement DrawerListener begin */
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }

    /* implement DrawerListener end */

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            switchDrawer();
        }
        return super.onKeyDown(keyCode, event);
    }

    @SuppressLint("InlinedApi")
    private void switchDrawer() {
        if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
            mDrawerLayout.closeDrawer(Gravity.START);
        } else {
            mDrawerLayout.openDrawer(Gravity.START);
        }
    }

    @SuppressLint("InlinedApi")
    void closeDrawer() {
        mDrawerLayout.closeDrawer(Gravity.START);
    }

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        GlobalTheme.applyTheme(this, true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        
        getSupportActionBar().setTitle(ViewUtil.getTitle(getText(R.string.app_name)));

        super.onCreate(savedInstanceState);

        Global.APP_CONTEXT = getApplicationContext();

        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
        mDrawerLayout.setDrawerListener(this);

        ListView drawer = (ListView) findViewById(R.id.left_drawer);
        DrawerAdapter drawerAdapter = new DrawerAdapter(this);
        drawer.setAdapter(drawerAdapter);
        drawer.setOnItemClickListener(drawerAdapter);

        IntentFilter filter = new IntentFilter();
        filter.addAction(DataManager.ACTION_DATA_CHANGED);
        filter.addAction(DataManager.ACTION_LOAD_DATA_FAILED);
        filter.addAction(DataManager.ACTION_START_LOAD_DATA);
        registerReceiver(mDataChangeBroadcastReceiver, filter);

        if (savedInstanceState == null) {
            switch2ItemPage();
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mDataChangeBroadcastReceiver);
        super.onDestroy();
    }

    void switch2ItemPage() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        ItemListFragment itemFragment = new ItemListFragment();
        itemFragment.setHasOptionsMenu(true);
        transaction.replace(R.id.fragment_container, itemFragment);
        transaction.commit();
    }

    void switch2CategoryPage() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        CategoryListFragment categoryListFragment = new CategoryListFragment();
        categoryListFragment.setHasOptionsMenu(true);
        transaction.replace(R.id.fragment_container, categoryListFragment);
        transaction.commit();
    }
}
